/* UTF8 Encoding */
/*-----------------------------------------------------------------------
|                            FILE DESCRIPTION                           |
-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
  - File name     : esp_01s_mqtt.h
  - Author        : liuzhihua (liuzhihuawy@163.com)
  - Update date   : 2024.04.12
  - Brief         : esp_01s mqtt AT firmware driver
  - Version       : v0.1
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               UPDATE NOTE                             |
-----------------------------------------------------------------------*/
/**
  * Update note:
  * ------------   ---------------   ----------------------------------
  *     Date            Author                      Note
  * ------------   ---------------   ----------------------------------
  *  2024.04.12       liuzhihua                  Create file          
***/

#ifndef ESP_01S_MQTT_H
#define ESP_01S_MQTT_H

/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include <stdint.h>

#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                                DEFINES                                |
-----------------------------------------------------------------------*/
#define ESP_01S_MQTT_RX_BUFFER_SIZE 256
typedef int (*esp_01s_mqtt_callback_t)(const uint8_t *data, uint16_t len);

    // char *ap_ssid;          //热点名字
    // char *ap_password;      //热点密码

    // char *station_ssid;     //连接的wifi名字
    // char *station_password; //连接的wifi密码
    // char *station_mac;      //连接的wifi mac地址

    // uint32_t local_ip;        //设备IP地址
    // uint16_t local_port;      //设备端口号
    // uint16_t state;         //设备状态

typedef enum
{
    ESP_01S_REQ_IDLE_STATE = 0x00,
    ESP_01S_REQ_WMODE_GET_STATE = 0x01,
    ESP_01S_REQ_IPSTA_GET_STATE = 0x02,
    ESP_01S_REQ_PING_GET_STATE = 0x03,
    ESP_01S_REQ_CONNECT_STATE = 0x04,     // 连接请求
    ESP_01S_REQ_PASSTHROUGH_STATE = 0x05, // 透传请求
    ESP_01S_REQ_JAP_STATE = 0x06,         // 加入AP请求
    ESP_01S_REQ_MQTT_CONNECT_STATE = 0x07, // MQTT连接请求

    ESP_01S_REQ_STATE = 253, // 通用请求状态
    ESP_01S_REQ_ERROR_STATE = 254, // 错误状态
    ESP_01S_REQ_CPLT_STATE = 255,  // 完成状态
} esp01s_request_state_t;

typedef enum
{
    ESP_01S_RECV_IDLE_STATE = 0x00,
    ESP_01S_RECV_START_STATE = 0x01,
    ESP_01S_RECV_END_STATE = 0x02,
    ESP_01S_RECV_ERROR_STATE = 0x03,
    ESP_01S_RECV_PASSTHROUGH_STATE = 0x04,
    ESP_01S_RECV_MQTTMSG_STATE = 0x05

}esp01s_receive_state_t;

typedef struct esp_01s_mqtt_typedef
{
    int (*uart_tx)(const uint8_t *data, uint16_t len);
    int (*uart_tx_printf)(const char *fmt, ...);
    esp_01s_mqtt_callback_t uart_rx_callback;
    esp01s_request_state_t request_state; // 请求状态
    esp01s_receive_state_t receive_state; // 接收状态
    uint8_t mqtt_connect;
    // uint8_t recv_state;
    char ip_device[16];
    char ip_gateway[16];
    char ip_netmask[16];
    uint16_t ping;
    uint8_t wirless_mode;
    uint8_t rxbuf[ESP_01S_MQTT_RX_BUFFER_SIZE]; //接收缓存
    uint8_t rxcnt;
}esp_01s_mqtt_t;


typedef struct mqtt_config_typedef
{
// <LinkID>：当前仅支持 link ID 0
// <scheme>：
    uint8_t scheme;
    // 1: MQTT over TCP；
    // 2: MQTT over TLS（不校验证书）；
    // 3: MQTT over TLS（校验 server 证书）；
    // 4: MQTT over TLS（提供 client 证书）；
    // 5: MQTT over TLS（校验 server 证书并且提供 client 证书）；
    // 6: MQTT over WebSocket（基于 TCP）；
    // 7: MQTT over WebSocket Secure（基于 TLS，不校验证书）；
    // 8: MQTT over WebSocket Secure（基于 TLS，校验 server 证书）；
    // 9: MQTT over WebSocket Secure（基于 TLS，提供 client 证书）；
    // 10: MQTT over WebSocket Secure（基于 TLS，校验 server 证书并且提供 client 证书）
    // uint8_t client_id_len;
    // uint8_t username_len;
    // uint8_t password_len;
// <client_id>：MQTT 客户端 ID，最大长度：256 字节
    char *client_id;
// <username>：用户名，用于登陆 MQTT broker，最大长度：64 字节
    char *username;
// <password>：密码，用于登陆 MQTT broker，最大长度：64 字节
    char *password;
// <cert_key_ID>：证书 ID，目前 ESP-AT 仅支持一套 cert 证书，参数为 0
    // uint8_t cert_key_ID;
// <CA_ID>：CA ID，目前 ESP-AT 仅支持一套 CA 证书，参数为 0
    // uint8_t CA_ID;
// <path>：资源路径，最大长度：32 字节
    char *host_ip;
    uint16_t host_port;

}mqtt_config_t;

#define ESP_01S_MAX_CONNECT_TYPE 3
extern const uint8_t ESP_01S_CONNECT_TYPE[ESP_01S_MAX_CONNECT_TYPE][4];
/*-----------------------------------------------------------------------
|                                  API                                  |
-----------------------------------------------------------------------*/
uint8_t *find_subData(const uint8_t *pdata, uint8_t size, const void *psubdata, uint8_t subdatasize);
void esp01s_request_set_idle(esp_01s_mqtt_t *pesp01s);
int esp01s_request_is_cplt(esp_01s_mqtt_t *pesp01s);
void esp01s_reset(esp_01s_mqtt_t *pesp01s);
int esp01s_get_wirless_mode_request(esp_01s_mqtt_t *pesp01s);
int esp01s_set_wirless_mode(esp_01s_mqtt_t *pesp01s, uint8_t mode, uint8_t auto_connect);

int esp01s_get_ip_state_request(esp_01s_mqtt_t *pesp01s);
int esp01s_ping(esp_01s_mqtt_t *pesp01s, const char *ip);


int esp01s_join_ap(esp_01s_mqtt_t *pesp01s, const char *ap_ssid, const char *ap_password, const char *bssid);
int esp01s_set_sap(esp_01s_mqtt_t *pesp01s, char *station_ssid, char *station_password, uint8_t channel, uint8_t ecn);

int esp01s_connect(esp_01s_mqtt_t *pesp01s, uint8_t type, const char *reomte_ip, uint16_t remote_port, uint16_t local_port, esp_01s_mqtt_callback_t pcallback);
int esp01s_disconnect(esp_01s_mqtt_t *pesp01s);
int esp01s_send(esp_01s_mqtt_t *pesp01s, uint8_t *data, uint16_t len);
int esp01s_set_passthrough(esp_01s_mqtt_t *pesp01s, uint8_t state);
int esp01s_set_transmit_mode(esp_01s_mqtt_t *pesp01s, uint8_t mode);
// AT+MQTTUSERCFG=0,1,"3fa85f641","571745621","b3fc2c963f66afa6",0,0,""
int esp01s_mqtt_userConfig(esp_01s_mqtt_t *pesp01s, const mqtt_config_t *mqttCfg);
int esp01s_mqtt_connect(esp_01s_mqtt_t *pesp01s,
                        const mqtt_config_t *mqttCfg,
                        esp_01s_mqtt_callback_t pcallback);
// int esp01s_mqtt_disconnect(esp_01s_mqtt_t *pesp01s);
int esp01s_mqtt_publish(esp_01s_mqtt_t *pesp01s, const char *topic, const char *data);
int esp01s_mqtt_subscribe(esp_01s_mqtt_t *pesp01s, const char *topic);
// int esp01s_mqtt_unsubscribe(esp_01s_mqtt_t *pesp01s, char *topic);
// int esp01s_uart_rx_callback(esp_01s_mqtt_t pesp01s, uint8_t *data, uint16_t len);
int esp01s_uart_rx_callback(esp_01s_mqtt_t *pesp01s, uint8_t *data, uint16_t len);

#ifdef __cplusplus
	}
#endif
#endif
